﻿namespace ReplayDataFile
{
    using System;
    using System.Collections.Generic;

    internal class DataTypeCachedInfo
    {
        private BlockFileReader bfReader;
        public List<DataRecord> CachedRecordList;
        public int DataType;
        private bool isFileReadOver = false;
        public int NextIndex;
        private int readCount = 0;
        public RecordIndex[] RecIndexs;

        public DataTypeCachedInfo(BlockFileReader bfReader)
        {
            this.bfReader = bfReader;
        }

        private void CachedRecord(long minRecordNo, long maxRecordNo)
        {
            if (!this.isFileReadOver && (this.GetCachedLastRecordNo() < maxRecordNo))
            {
                while (true)
                {
                    this.ReadToCache();
                    if (this.isFileReadOver || (this.GetCachedLastRecordNo() >= maxRecordNo))
                    {
                        return;
                    }
                }
            }
        }

        private long GetCachedLastRecordNo()
        {
            if (this.GetRemainRecordCount() > 0)
            {
                DataRecord record = this.CachedRecordList[this.CachedRecordList.Count - 1];
                return record.RecordNo;
            }
            return 0L;
        }

        public int GetRemainRecordCount()
        {
            return (this.CachedRecordList.Count - this.readCount);
        }

        public bool IsReadOver()
        {
            return (this.isFileReadOver && (this.GetRemainRecordCount() == 0));
        }

        public List<DataRecord> ReadCachedRecord(long minRecordNo, long maxRecordNo)
        {
            List<DataRecord> list = new List<DataRecord>();
            if (!this.IsReadOver())
            {
                this.CachedRecord(minRecordNo, maxRecordNo);
                int readCount = this.readCount;
                while (readCount < this.CachedRecordList.Count)
                {
                    DataRecord item = this.CachedRecordList[readCount];
                    if (item.RecordNo > maxRecordNo)
                    {
                        break;
                    }
                    if ((item.RecordNo >= minRecordNo) && (item.RecordNo <= maxRecordNo))
                    {
                        list.Add(item);
                    }
                    readCount++;
                }
                this.readCount = readCount;
            }
            return list;
        }

        private void ReadToCache()
        {
            if (!this.isFileReadOver)
            {
                this.CachedRecordList.RemoveRange(0, this.readCount);
                this.readCount = 0;
                RecordIndex beginIndex = null;
                RecordIndex endIndex = null;
                if (this.NextIndex >= this.RecIndexs.Length)
                {
                    this.isFileReadOver = true;
                }
                else
                {
                    int num = 10;
                    beginIndex = this.RecIndexs[this.NextIndex];
                    int index = this.NextIndex + num;
                    endIndex = (index >= this.RecIndexs.Length) ? null : this.RecIndexs[index];
                    List<DataRecord> collection = this.bfReader.Read(this.DataType, beginIndex, endIndex);
                    this.CachedRecordList.AddRange(collection);
                    this.NextIndex += num;
                    if (endIndex == null)
                    {
                        this.isFileReadOver = true;
                    }
                }
            }
        }
    }
}

